define([
    "dojo/_base/declare",
    "dijit/_Widget",
    "dijit/_Templated",
    "dojo/text!./SupplyForm/template/form.html",
    "dojo/_base/kernel",
    "dijit/form/TextBox",
    "dijit/form/Select",
    "dijit/form/Button",
    "dijit/form/Form",
    "dijit/Dialog",
    "dojox/form/FileInput",
    "dojo/io/iframe",
    "dijit/form/SimpleTextarea"],
    function(declare, _Widget,_Templated,templateHtml,dojo,TextBox){
        return declare("widget.SupplyForm",[_Widget,_Templated], {
            widgetsInTemplate: true,
            baseClass: "supply-form",
            templateString: templateHtml,
            readOnly:false,
            postCreate: function(){
                this.inherited(arguments);
                this.bindAction();
                dojo.connect(this.supplyTypeSelect,"onChange",dojo.hitch(this,this.loadFactors));
                this.loadSupplyTypes();
            },
            
            loadSupplyTypes:function(){
                var supplyTypes = this.supplyTypeSelect;
                var xhrArgs = {
                    url:"supply/getsupplytype",
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(function(result){
                    var options = [];
                    for(var i in result){
                        options.push({value:result[i].ID,label:result[i].Name});
                    }
                    supplyTypes.addOption(options);
                });
            },
            
            loadFactors:function(){
                var typeid = this.supplyTypeSelect.get("value");
                var supplyid = this.supplyId.get("value");
                var xhrArgs = {
                    url:"supply/getsupplytypefactor/supplytypeid/"+typeid+(supplyid==""?"":("/supplyid/"+supplyid)),
                    handleAs: "json"
                };
                var factorsNode = this.factorsForm;
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(function(result){
                    result = result.factors;
                    factorsNode.innerHTML = "";
                    for(var i in result){
                        var box = new TextBox({
                            style: 'width:120px',
                            value: result[i].RankingGrade,
                            rule: result[i].ID
                        });
                        var div = dojo.create("div",{"class":"row"},factorsNode);
                        dojo.create("label",{innerHTML:result[i].Subcat},div);
                        dojo.place(box.domNode,div);
                        dojo.create("label",{innerHTML:"&nbsp;&nbsp;&nbsp;" + result[i].MainCat},div);
                    }
                }).then(dojo.hitch(this,function(){
                    this.setMode();
                }));
            },
            
            loadSupply:function(id){
                var xhrArgs = {
                    url:"supply/getsupply/sid/"+id,
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    this.reset();
                    dijit.byId("supplyForm1").set("value",result[0]);
                    this.updatedTime.innerHTML = result[0].UpateTime;
                    this.createdTime.innerHTML = result[0].CreateTime;
                    this.photo.src = djConfig.image + result[0].Photo;
                    this.loadFactors();
                }));
            },
            
            save:function(){
                var inputs = dijit.byId("supplyForm2").getChildren();
                if (inputs && inputs.length>0){
                    var factorString = [];
                    for(var i = 0; i<inputs.length ; i++){
                        factorString.push(inputs[i].get("rule")+","+inputs[i].get("value"));
                    }
                    this.factor.set("value",factorString.join(";"));
                }else{
                    this.factor.set("value","");
                }
                var xhrArgs = {
                    url: "supply/save",
                    form: "supplyForm1",
                    handleAs: "json"
                };
                var deferred = dojo.xhrPost(xhrArgs);
                deferred.then(dojo.hitch(this,function(result){
                    this.loadSupply(result.id);
                    alert(result.message);
                }),function(error){
                    alert(error);
                });
            },
            
            bindAction: function(){
                dojo.connect(this.backButton,"onClick",dojo.hitch(this,function(){
                    dojo.style(this.domNode, "display", "none");
                    dojo.publish("SupplyList/Show");
                }));
                
                dojo.connect(this.saveButton,"onClick",dojo.hitch(this,this.save));
                
                dojo.connect(this.uploadButton,"onClick",dojo.hitch(this,function(){
                    dijit.byId("supplyForm3").reset();
                    var id = this.supplyId.get("value");
                    if (!id){
                        alert("Please save first.");
                        return;
                    }
                    this.uploadSupplyId.set("value",id);
                    this.uploadDialog.show();
                }));
                
                dojo.connect(this.doUploadButton,"onClick",dojo.hitch(this,function(){
                    
                    dojo.io.iframe.send({
                        form: "supplyForm3",
                        handleAs: "text",
                        load: dojo.hitch(this,function(response, ioArgs){
                            this.photo.src = djConfig.image + response;
                            this.uploadDialog.hide();
                        }),
                        error: function(response, ioArgs){
                            alert("error:"+response);
                        }
                    });
                }));

                dojo.subscribe("SupplyForm/Show", null,dojo.hitch(this,function(object){
                    if (object.mode == "view"){
                        this.view(object.id);
                    }else if (object.mode == "edit"){
                        this.edit(object.id);
                    }else if (object.mode == "new"){
                        this.newSupply();
                    }
                    dojo.style(this.domNode, "display", "block");
                }));
            },
            
            view: function(id){
                this.readOnly = true;
                this.setMode();
                this.loadSupply(id);
            },
            
            edit:function(id){
                this.readOnly = false;
                this.setMode();
                this.loadSupply(id);
            },
            
            newSupply :function(){
                this.readOnly = false;
                this.setMode();
                this.reset();
            },
            
            reset:function(){
                var form1 = dijit.byId("supplyForm1");
                var form2 = dijit.byId("supplyForm2");
                var form1Inputs = form1.getChildren();
                for (var i =0;i<form1Inputs.length;i++){
                    form1Inputs[i].set("value","");
                }
                var form2Inputs = form2.getChildren();
                for (var i =0;i<form2Inputs.length;i++){
                    form2Inputs[i].set("value","");
                }
                this.photo.src = '';
                this.createdTime.innerHTML = "";
                this.updatedTime.innerHTML = "";
            },
            
            setMode:function(){
                var form1 = dijit.byId("supplyForm1");
                var form2 = dijit.byId("supplyForm2");

                var form1Inputs = form1.getChildren();
                for (var i =0;i<form1Inputs.length;i++){
                    form1Inputs[i].set("disabled",this.readOnly);
                }
                var form2Inputs = form2.getChildren();
                for (var i =0;i<form2Inputs.length;i++){
                    form2Inputs[i].set("disabled",this.readOnly);
                }

                this.saveButton.set("disabled",this.readOnly);
                this.uploadButton.set("disabled",this.readOnly);
            }
            
        });
    });